/**
 * Created by zy on 2018/8/16.
 */
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by will on 2017/6/9.
 * 使用poi将word转为html文件，并从文件中读取内容
 */
public class PoiUtil {
    // 在html中图片保存的相对路径
    private static String imagePath;

    /**
     * @param source word文件的File对象
     * @param sourceFileName word文件名
     * @param savePath 图片保存路径
     * @return 转成的html字符串
     */
    public static String getHtml(File source, String sourceFileName, String savePath) throws Exception {
        imagePath = "/upload/" + sourceFileName.substring(0, sourceFileName.lastIndexOf("."));
        String imagePathStr = savePath + File.separator + sourceFileName.substring(0, sourceFileName.lastIndexOf(".")) + File.separator;
        String content;
        String imgEnd = "";
        // 判断word文档类型，使用不同方法进行转换
        if (sourceFileName.endsWith(".doc")) {
            content = docToStr(source, sourceFileName, imagePathStr);
        } else if (sourceFileName.endsWith(".docx")) {
            content = docxToStr(source, sourceFileName, imagePathStr);
            // 转换docx文件得到的图片路径
            imgEnd = "word/media/";
        } else {
            return "文件类型错误";
        }
        // 利用正则表达式过滤无用标签和属性
        content = RegexAnswerUtil.clear(content);
        return content;
    }

    // doc转换为html
    public static String docToStr(File source, String sourceFileName, String imagePathStr) throws Exception {
        String targetFileName = imagePathStr + sourceFileName.substring(0, sourceFileName.lastIndexOf(".")) + ".html";
        File target = new File(targetFileName);
        target.getParentFile().mkdirs();
        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(source));
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
        // 保存图片，并返回图片的相对路径
        wordToHtmlConverter.setPicturesManager((content, pictureType, name, width, height) -> {
            try (FileOutputStream out = new FileOutputStream(new File(imagePathStr + name))) {
                out.write(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return imagePath +"/" + name;
        });
        wordToHtmlConverter.processDocument(wordDocument);
        Document htmlDocument = wordToHtmlConverter.getDocument();
        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(new File(targetFileName));
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        String content = splitContext(targetFileName);
        // 删除生成的html文件
        File file = new File(targetFileName);
        file.delete();
        return content;
    }

    // docx转换为html
    public static String docxToStr(File source, String sourceFileName, String imagePathStr) throws Exception {
        String targetFileName = imagePathStr + sourceFileName.substring(0, sourceFileName.lastIndexOf(".")) + ".html";
        File target = new File(targetFileName);
        target.getParentFile().mkdirs();
        OutputStreamWriter outputStreamWriter = null;
        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream(source));
            XHTMLOptions options = XHTMLOptions.create();
            // 存放图片的文件夹
            options.setExtractor(new FileImageExtractor(new File(imagePathStr)));
            // html中图片的路径
            options.URIResolver(new BasicURIResolver(imagePath));
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(target), "utf-8");
            XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
            xhtmlConverter.convert(document, outputStreamWriter, options);
        } finally {
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
        }
        String content = splitContext(targetFileName);
        // 删除生成的html文件
        File file = new File(targetFileName);
        //file.delete();
        return content;
    }

    /**
     * docx文件转html会生成html编码
     * 该方法能转换大部分
     * 富文本编辑器中可以不做处理
     */
    public static String htmlEncoding(String html) {
        String regExp = "&#\\d*;";
        Matcher m = Pattern.compile(regExp).matcher(html);
        StringBuffer sb = new StringBuffer();
        if (!m.find()) {
            sb.append(html);
        } else {
            while (m.find()) {
                String s = m.group(0);
                s = s.replaceAll("(&#)|;", "");
                char c = (char) Integer.parseInt(s);
                m.appendReplacement(sb, Character.toString(c));
            }
        }
        return sb.toString();
    }

    /**
     * 读取转换得到的html文件，并过滤多余空行
     */
    public static String splitContext(String filePath) {
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString);
                if(!tempString.equals("")){
                    sb.append("\n");
                }
            }
            reader.close();
            String content = sb.toString().replaceAll("\\n+", "\n");
            return content;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return "";
    }
}